const int LEDS[] = {3,5,7};
const int BUTTONS[] = {2,4,6};

int score = 0;
bool gameStart = false;
unsigned long gameStartTime;
const unsigned long gameDuration = 6000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for(int i = 0;i<3;i++){
    pinMode(LEDS[i],OUTPUT);
    pinMode(BUTTONS[i],INPUT_PULLUP);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if(!gameStart&&digitalRead(BUTTONS[0])==LOW&&digitalRead(BUTTONS[1])==LOW&&digitalRead(BUTTONS[2])==LOW){
    Serial.println("游戏开始");
    gameStart = true;
    score = 0;
    gameStartTime = millis();
    delay(1000);
  }
  if(gameStart&&millis()-gameStartTime<gameDuration){
    int led = random(0,3);
    digitalWrite(LEDS[led],HIGH);
    long startTime = millis();
    bool buttonPressed = false;

    while(millis()-startTime<1000){
      if(digitalRead(BUTTONS[led])==LOW){
        buttonPressed = true;
        break;
      }
    }
    digitalWrite(LEDS[led],LOW);

    if(buttonPressed){
      score++;
      Serial.println("得分+1");
    }
    delay(200);
  }else if(gameStart){
    gameStart = false;
    Serial.print("游戏结束，你的得分：");
    Serial.println(score);
    delay(3000);
    }
}
